package Calculation;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: Hua YY
 * Date: 2024-10-07
 * Time: 10:59
 */
class Solution7 {

        public List<List<Integer>> threeSum(int[] nums) {
            Arrays.sort(nums);
            int n = nums.length;
            List<List<Integer>> ret = new ArrayList<>();
            for(int i = 0 ; i < n ;){
                if(nums[i] > 0){
                    break;
                }
                int left = i+1 , right = n-1 ,target = -nums[i];
                while(left < right){
                    int sum = sum(nums[left] , nums[right]);
                    if(sum > target){
                        right--;
                    }else if(sum < target){
                        left++;
                    }else{
                        ret.add(Arrays.asList(nums[left] , nums[right] , nums[i]));
                        left++;
                        right--;
                        while(left < right && nums[left] == nums[left - 1]){
                            left++;
                        }
                        while(left < right && nums[right] == nums[right + 1]){
                            right--;
                        }
                    }
                }
                i++;
                while(i < n && nums[i] == nums[i-1]){
                    i++;
                }

            }
            return ret;

        }
        public int sum(int a , int b){
            return a+b;
        }

}
public class Test7 {
}
